package com.baizhi;

import com.baizhi.service.BookService;
import com.baizhi.service.impl.BookServiceImpl;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CartAction {
    private  int  book_id;
    static Map<Integer, CartItem>  buyCarts;
    static Map<Integer,CartItem>  removeCart;
    private CartItem cartItem=new CartItem();
    private Book  book;
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    private  BookService  bs=new BookServiceImpl();
    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public int getBook_id() {
        return book_id;
    }

    public void setBook_id(int book_id) {
        this.book_id = book_id;
    }

    public Map<Integer, CartItem> getBuyCarts() {
        return buyCarts;
    }

    public void setBuyCarts(Map<Integer, CartItem> buyCarts) {
        this.buyCarts = buyCarts;
    }

    public Map<Integer, CartItem> getRemoveCart() {
        return removeCart;
    }

    public void setRemoveCart(Map<Integer, CartItem> removeCart) {
        this.removeCart = removeCart;
    }

    public CartItem getCartItem() {
        return cartItem;
    }

    public void setCartItem(CartItem cartItem) {
        this.cartItem = cartItem;
    }

    public void Cart()  {
        HttpServletResponse response = ServletActionContext.getResponse();
        book = bs.selectOne(book_id);//获取图书的信息
        int sum=0;
        double total=0;
        double save=0;
        if (buyCarts==null){
           buyCarts=new HashMap<>();
        }
        Cart cart = (Cart) ServletActionContext.getRequest().getSession().getAttribute("cart");//将cart对象存入session中
        if (cart==null){
            cart = new Cart();//如果cart为空  重新new出来一个
            cartItem.setBook(book);
            cartItem.setCount(1);
            buyCarts.put(book_id,cartItem);
        }else{
             if (buyCarts.containsKey(book_id)){
                 CartItem cartItem = buyCarts.get(book_id);
                 cartItem.setCount(cartItem.getCount()+1);
             }else{
                cartItem.setCount(1);
                cartItem.setBook(book);
                buyCarts.put(book_id,cartItem);
             }
        }
        Set<Map.Entry<Integer, CartItem>> entries = buyCarts.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            sum =  entry.getValue().getCount();
            double  dang1 = entry.getValue().getBook().getDangPrice();
            double price1 = entry.getValue().getBook().getPrice();
            total+=sum*dang1;
            save+=sum*(price1-dang1);
        }
        System.out.println(sum);
        cart.setTotalMoney(total);
        cart.setSaveMoney(save);
        cart.setBuyMap(buyCarts);
        ServletActionContext.getRequest().getSession().setAttribute("cart",cart);
        ServletActionContext.getRequest().getSession().setAttribute("c", buyCarts);
        try {
            PrintWriter writer = response.getWriter();
            writer.print("true");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String remove(){
        if (removeCart==null){
            removeCart=new HashMap<>();
        }
        //获取session中cart对象
        Cart cart = (Cart)ServletActionContext.getRequest().getSession().getAttribute("cart");
        Map<Integer, CartItem> buyCart =(Map<Integer, CartItem>) ServletActionContext.getRequest().getSession().getAttribute("c");
        int sum=0;
        int count = buyCart.get(book_id).getCount();
        double save=cart.getSaveMoney();
        double total=cart.getTotalMoney();
        System.out.println(count);
        CartItem item = cart.getBuyMap().remove(book_id);
        book=bs.selectOne(book_id);
        if (removeCart.containsKey(book_id)){
            CartItem cartItem = removeCart.get(book_id);
            cartItem.setCount(cartItem.getCount()+1);
            System.out.println(cartItem.getCount());
        }else{
            cartItem =new CartItem();
            cartItem.setBook(book);
            cartItem.setCount(count);
            removeCart.put(book_id, cartItem);
        }
        Set<Map.Entry<Integer, CartItem>> entries = removeCart.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            sum =  entry.getValue().getCount();
            double  dang1 = entry.getValue().getBook().getDangPrice();
            double price1 = entry.getValue().getBook().getPrice();
            total-=sum * dang1;
            save-=sum*(price1-dang1);
        }
        System.out.println(cart);
        System.out.println(removeCart.get(book_id).getCount());
        System.out.println(total);
        System.out.println(save);
        if (save<0&&total<0){
            save=0;
            total=0;
        }
        CartItem remove = buyCarts.remove(book_id);
        cart.setTotalMoney(total);
        cart.setSaveMoney(save);
        ServletActionContext.getRequest().getSession().setAttribute("cart",cart);
        ServletActionContext.getRequest().getSession().setAttribute("remove",removeCart);
        return "remove";
    }
    public String huifu(){
        BookService  bs=new BookServiceImpl();
        book = bs.selectOne(book_id);
        cartItem.setBook(book);
        Cart cart =(Cart) ServletActionContext.getRequest().getSession().getAttribute("cart");
        Map<Integer, CartItem> buyCart =(Map<Integer, CartItem>) ServletActionContext.getRequest().getSession().getAttribute("c");
        Map<Integer, CartItem> removeCart=(Map<Integer, CartItem>) ServletActionContext.getRequest().getSession().getAttribute("remove");
        buyCart.put(book_id,cartItem);
        System.out.println(removeCart);
        double total = 0;
        double save =0;
        int count = removeCart.get(book_id).getCount();
        buyCart.get(book_id).setCount(count);
        Set<Map.Entry<Integer, CartItem>> entries = buyCart.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            double  sum =  entry.getValue().getCount();
            double  dang1 = entry.getValue().getBook().getDangPrice();
            double price1 = entry.getValue().getBook().getPrice();
            total+=sum * dang1;
            save+=sum*(price1-dang1);
        }
        removeCart.remove(book_id);
        cart.setSaveMoney(save);
        cart.setTotalMoney(total);
        ServletActionContext.getRequest().getSession().setAttribute("c",buyCart);
        ServletActionContext.getRequest().getSession().setAttribute("remove",removeCart);
        return "huifu";
    }
    public String biangeng(){
        Cart cart =(Cart) ServletActionContext.getRequest().getSession().getAttribute("cart");
        Map<Integer, CartItem> buyCart =(Map<Integer, CartItem>) ServletActionContext.getRequest().getSession().getAttribute("c");
        buyCart.get(book_id).setCount(count);
        double total = 0;
        double save =0;
        Set<Map.Entry<Integer, CartItem>> entries = buyCart.entrySet();
        for (Map.Entry<Integer, CartItem> entry : entries) {
            double  sum =  entry.getValue().getCount();
            double  dang1 = entry.getValue().getBook().getDangPrice();
            double price1 = entry.getValue().getBook().getPrice();
            total+=sum * dang1;
            save+=sum*(price1-dang1);
        }
        cart.setSaveMoney(save);
        cart.setTotalMoney(total);
        ServletActionContext.getRequest().getSession().setAttribute("c",buyCart);
        return "biangeng";
    }
}
